﻿using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Helper;
using SIG.InputModel.Front.Emails;
using SIG.Resources.Front;
using SIG.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class ConsultationController : BaseController
    {
        Infrastructure.Email.IEmailService _emailService;
        //EmailTemplateRepository _templateService;
        EmailRepository _emailListService;
        EmailAccountRepository _emailAccountRepository;
        public ConsultationController(Infrastructure.Email.IEmailService emailService)
        {
            _emailService = emailService;
            //_templateService = new EmailTemplateRepository();
            _emailListService = new EmailRepository();
            _emailAccountRepository = new EmailAccountRepository();
        }
        // GET: Consultation
        public ActionResult Index()
        {
            ConsultationIM Consultation = new ConsultationIM();
            //   Consultation.AvailableAreas = new List<string>() { "电子信息技术", "生物与新医药技术", "航空航天技术", "新材料技术", "新能源及节能技术", "资源与环境技术", "高新技术改造传统产业", "其他领域" };
            return View(Consultation);
        }
        [HttpPost]
        public JsonResult SendEmail(ConsultationIM im)
        {
           // EmailIM vm = new EmailIM();
          //  EmailVM emailVM = new EmailVM();
            //string mailto = BBICMS.Infrastructure.Helpers.Settings.ContactForm.MailTo;
            //string mailcc = BBICMS.Infrastructure.Helpers.Settings.ContactForm.MailCC;

            var subject = SettingsManager.Site.SiteName + "申报咨询";

            string areas = "";
            if (im.Areas != null)
            {
                foreach (string area in im.Areas)
                {
                    areas += area + "，";
                }
            }
            else
            {
                return Json("1", JsonRequestBehavior.DenyGet);
            }

            string emailBody = string.Empty;

            emailBody += "企业名称：" + im.CompanyName + "<br/>";
            emailBody += "企业主营业务：" + im.CompanyIntro + "<br/>";
            emailBody += "联系人：" + im.ContactMan + "<br/>";
            emailBody += "电话：" + im.Phone + "<br/>";
            emailBody += "手机：" + im.Mobile + "<br/>";
            emailBody += "QQ：" + im.OICQ + "<br/>";
            emailBody += "电子邮箱：<a href='mailto:" + im.Email + "'>" + im.Email + "</a><br/>";
            emailBody += "主营产品（服务）所属技术领域：" + areas + "<br/>";

            emailBody += "专利：" + im.Patent.ToString() + "<br/>";
            emailBody += "实用新型：" + im.Newtype.ToString() + "<br/>";
            emailBody += "外观设计：" + im.Design.ToString() + "<br/>";
            emailBody += "软件著作权：" + im.Software.ToString() + "<br/>";
            emailBody += "集成电路布图设计专有权：" + im.Integrate.ToString() + "<br/>";
            emailBody += "植物新品种：" + im.Plant.ToString() + "<br/>";
            emailBody += "其他：" + im.Orther + "<br/>";

            emailBody += "职工总数（人）：" + im.WorkerCount + "<br/>";
            emailBody += "大专以上学历科技人员数（人）：" + im.BrainpowerCount + "<br/>";
            emailBody += "从事研究开发人员数（人）：" + im.DeveloperCount + "<br/>";
            emailBody += "留学归国人员数（人）：" + im.OverseasCount + "<br/>";

            emailBody += "第一年销售收入：" + string.Format("{0:c}", im.SaleOne) + "<br/>";
            emailBody += "第二年销售收入：" + string.Format("{0:c}", im.SaleTwo) + "<br/>";
            emailBody += "第三年销售收入：" + string.Format("{0:c}", im.SaleThree) + "<br/>";
            emailBody += "第一年总资产：" + string.Format("{0:c}", im.AssetsOne) + "<br/>";
            emailBody += "第二年总资产：" + string.Format("{0:c}", im.AssetsTwo) + "<br/>";
            emailBody += "第三年总资产：" + string.Format("{0:c}", im.AssetsThree) + "<br/>";


            var emailAccount = _emailAccountRepository.GetDefaultAccount();
            if (emailAccount == null)
            {
                AR.Setfailure("请选设置邮件默认发送帐号");
                return Json(AR, JsonRequestBehavior.DenyGet);

                
            }
            try
            {
                _emailService.SendMail(im.CompanyName, im.Email, SettingsManager.Contact.MailTo, SettingsManager.Contact.MailCC,
                subject, emailBody, emailAccount.Smtpserver, emailAccount.Email, SettingsManager.Site.SiteName,
                emailAccount.UserName, EncryptionHelper.Decrypt(emailAccount.Password), (int)emailAccount.Port, emailAccount.EnableSsl);

                Email email = new Email
                {
                    Body = emailBody,
                    Subject = subject,
                    MailTo = im.Email,
                    MailCc = SettingsManager.Contact.MailCC,
                    Active = true,
                    CreatedBy = im.CompanyName,
                    CreatedDate = DateTime.Now
                };

                _emailListService.Create(email);

                AR.SetSuccess(Messages.EmailSentSuccess);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception ex)
            {
                AR.Setfailure(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }


        }
    }
}